package dj.common.support;

import com.dj.common.constants.ResponseStatus;
import com.dj.common.response.CentralResponse;
import org.apache.commons.lang3.ObjectUtils;

/**
 * <br>
 * <p>中心部件响应数据转换处理handle</p>
 *
 * <br>
 * @author ZJG
 * @version 1.0
 * @date 2021/7/10 下午2:34
 */
public final class ResponseHandler {


    /**
     * 处理成功
     * 返回无提示信息的中心部件response对象
     * {@link CentralResponse}
     * @return
     */
    public static <TDest> CentralResponse<TDest> success() {
        final CentralResponse<TDest> response = new CentralResponse<>();
        return response;
    }

    /**
     * 处理成功
     * 返回无提示信息的中心部件response对象
     * {@link CentralResponse}
     * @return
     */
    public static <TDest> CentralResponse<TDest> success(String message) {
        final CentralResponse<TDest> response = new CentralResponse<>();
        response.setMsg(message);
        return response;
    }

    /**
     * 处理成功
     * 返回无提示信息的中心部件response对象
     * {@link CentralResponse}
     * @return
     */
    public static <TDest> CentralResponse<TDest> success(TDest dest) {
        final CentralResponse<TDest> response = new CentralResponse<>();
        response.setData(dest);
        return response;
    }

    /**
     * 处理失败
     * 返回无提示信息的中心部件response对象
     * {@link CentralResponse}
     *
     * @return
     */
    public static <TDest> CentralResponse<TDest> fail() {
        final CentralResponse<TDest> response = new CentralResponse<>();
        response.setCode(500);
        response.setMsg("fail");
        return response;
    }

    /**
     * 处理失败
     * 返回带有提示msg的中心部件response对象
     * {@link CentralResponse}
     *
     * @param message
     * @return
     */
    public static <TDest> CentralResponse<TDest> fail(String message) {
        final CentralResponse<TDest> response = new CentralResponse<>();
        response.setCode(500);
        response.setMsg(message);
        return response;
    }

    /**
     * 处理失败
     * 返回带有提示msg的中心部件response对象
     * {@link CentralResponse}
     *
     * @param tDest
     * @return
     */
    public static <TDest> CentralResponse<TDest> fail(TDest tDest) {
        final CentralResponse<TDest> response = new CentralResponse<>();
        response.setCode(500);
        response.setMsg("request is failure.");
        response.setData(tDest);
        return response;
    }

    /**
     * 处理失败
     * 返回带有提示msg的中心部件response对象
     * {@link CentralResponse}
     *
     * @param message
     * @param throwable
     * @return
     */
    public static <TDest> CentralResponse<TDest> fail(String message, TDest throwable) {
        final CentralResponse<TDest> response = new CentralResponse<>();
        response.setCode(500);
        response.setMsg(message);
        response.setData(throwable);
        return response;
    }

    /**
     * 处理失败
     * 返回带有提示code&msg的中心部件response对象
     * {@link CentralResponse}
     *
     * @param code
     * @param message
     * @return
     */
    public static <TDest> CentralResponse<TDest> fail(int code, String message) {
        code = code == 0? ResponseStatus.HTTP_INTERNAL_ERROR : code;
        final CentralResponse<TDest> response = new CentralResponse<>();
        response.setCode(Integer.valueOf(code));
        response.setMsg(message);
        return response;
    }

    /**
     * 返回中心部件response对象
     * {@link CentralResponse}
     *
     * @param result
     * @param <TDest>
     * @return
     */
    public static <TDest> CentralResponse<TDest> toResponse(TDest result) {
        final CentralResponse<TDest> response = new CentralResponse<>();
        response.setData(result);
        return response;
    }

    /**
     * 转为单响应数据对象
     * {@link CentralResponse}
     *
     * @param result
     * @param converter
     * @param <TSrc>
     * @param <TDest>
     * @return
     */
    public static <TSrc, TDest> CentralResponse<TDest> toResponse(TSrc result,
                                                                  ResponseConverter<TSrc, TDest> converter) {
        final CentralResponse<TDest> response = new CentralResponse<>();
        TDest tDest = ObjectUtils.isNotEmpty(result) ? converter.convert(result) : null;
        response.setData(tDest);
        return response;
    }

    /**
     * 由源中心部件response转换到目标中心部件响应数据对象
     * {@link CentralResponse}
     *
     * @param source
     * @param converter
     * @param <TSrc>
     * @param <TDest>
     * @return
     */
    public static <TSrc, TDest> CentralResponse<TDest> toResponse(final CentralResponse<TSrc> source,
                                                                  final ResponseConverter<TSrc, TDest>  converter) {
        return ObjectUtils.isNotEmpty(source) ? toResponse(source.getData(), converter) : new CentralResponse<TDest>();
    }

}
